<template>
  <div class="h-full overflow-hidden w-full">
    <router-view></router-view>
  </div>
</template>
<route>
  {
    name: "admin",
    meta: {
      pageIndex: 0,
    }
  }
</route>
<script setup lang="ts">
import $bus from "@/utils/bus";
import { cloneDeep } from "lodash-es";

import { SocketMsgType } from "@/enums";
import { useGameStore } from "@/store/modules/game";
import { useUserStore } from "@/store/modules/user";
import { useRoomStore } from "@/store/modules/room";
import { useSocketStore } from "@/store/modules/socket";
import { useKefuStore } from "@/store/modules/kefu";
import { storeToRefs } from "pinia";
import { usePremsStore } from "@/store/modules/prems";
import { toast } from "@/utils/toast";

const premsStore = usePremsStore();
const userStore = useUserStore();
const gameStore = useGameStore();
const roomStore = useRoomStore();
const socketStore = useSocketStore();
const kefuStore = useKefuStore();
const { userInfo } = storeToRefs(userStore);
const { gameTotal } = storeToRefs(gameStore);
const route = useRoute();

// 监听socket连接状态
const watckSocket = () => {
  socketStore.client?.subscribe("chatmessage", data => {
    console.log("---房间消息---", data);
    if (data?.msg_type == SocketMsgType.open) {
      const msgData = data.data as SocketOpenMsg;
      const games = cloneDeep(gameTotal.value);
      const cur = games.find(item => item.LotteryId == msgData.Id);
      const newLotteryData = {
        ...cur,
        BetAmount: cur?.BetAmount,
        CloseTime: msgData.CloseTime,
        Issue: msgData.CurrentBitchNo,
        LastNum: msgData.LastNum,
        LuckCode: msgData.LuckCode,
        NextTime: msgData.NextTime
      };
      gameStore.setLotteryGame(newLotteryData as GameTotalMo);
      $bus.emit("open-result", newLotteryData as GameTotalMo);
      $bus.emit("update-total-result", newLotteryData as GameTotalMo);
    } else if (
      data?.msg_type == SocketMsgType.room_text_msg ||
      data?.msg_type == SocketMsgType.img_msg ||
      data?.msg_type == SocketMsgType.room_guessing_msg
    ) {
      $bus.emit("room-msg", data as GameHistoryMo);
    }
  });
  socketStore.client?.subscribe("error_msg", (data: any) => {
    toast.info(data.message as any);
  });
  kefuStore.client?.subscribe("chatmessage", data => {
    console.log("---私聊消息---", data);
    if (data?.msg_type == SocketMsgType.kefu_text_msg || data?.msg_type == SocketMsgType.img_msg) {
      $bus.emit("kefu-msg", data as KefuMsg);
      if (data.user_id !== `r_${userInfo.value?.RoomId}`) {
        if (premsStore.isEditChatSetting && route.path != "/admin/main/chat-room") {
          $bus.emit("kefu-msg-tip", data as KefuMsg);
        }
      }
    } else if (
      data?.msg_type == SocketMsgType.up_score ||
      data?.msg_type == SocketMsgType.down_socore ||
      data?.msg_type == SocketMsgType.joinroom
    ) {
      $bus.emit("updown-join-tip", data as SocketMsg);
    } else if (data?.msg_type == SocketMsgType.game_bet) {
      $bus.emit("game-bet", data.data as GameBetMo);
    } else if (
      data?.msg_type == SocketMsgType.single_bet ||
      data?.msg_type == SocketMsgType.single_win ||
      data?.msg_type == SocketMsgType.follow_up
    ) {
      $bus.emit("single-follow-tip", data as SocketMsg);
    } else if (data?.msg_type == SocketMsgType.updata_history_list) {
      $bus.emit("updata-history-list");
    }
  });
};

onMounted(async () => {
  if (!window.isFirst) {
    if (userStore.getToken) {
      if (gameStore.gameTotal.length == 0) {
        gameStore.actionGameTotal();
      }
      gameStore.actionGameLeft();
      roomStore.actionRoomInfo();
      !socketStore.client && (await socketStore.initSocket());
      !kefuStore.client && (await kefuStore.initSocket());
      watckSocket();
    }
    window.isFirst = true;
  }
});
</script>
<style>
.my-toast > div {
  width: 100%;
}
</style>
